package com.codingInn.web.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@RequestMapping("/book")
@SessionAttributes(value = {"book","description"},types = {Double.class})
public class BookController {
	private final Log logger = LogFactory.getLog(BookController.class);

	@RequestMapping("/index")
	public String index(Model model) throws Exception{
		model.addAttribute("book","C++ primer");
		model.addAttribute("description","C plus plus");
		model.addAttribute("price",new Double("100"));
		return "redirect:get";
	}

	@RequestMapping("/get")
	public String getBySessionAttributes(@ModelAttribute("book") String book,Model model, ModelMap modelMap, SessionStatus sessionStatus) throws Exception{
		Object price = ((Map) model).get("price");
		Object price2 = ((Map<?, ?>) model).get("price");
		ModelAndView modelAndView = new ModelAndView();

//		modelAndView.addObject(model);
		modelAndView.addAllObjects(model.asMap());
		logger.info("===============getBySessionAttributes===============");
		logger.info("get by @ModelAttribute:" + book);
		logger.info("get by ModelMap:"+modelMap.get("book")+":"+modelMap.get("description")+","+modelMap.get("price"));
		sessionStatus.setComplete();
		return "redirect:complete";
	}

	@RequestMapping("/complete")
	public String afterComplete(ModelMap modelMap,Model model) throws Exception{
		logger.info("===============afterComplete===============");
		logger.info(modelMap.get("book")+":"+modelMap.get("description")+","+modelMap.get("price"));
		return "go";
	}
}
